<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Cities;
use App\Models\FileUpload;
use App\Models\FilmTypeMaster;
use App\Models\MacroManagement;
use App\Models\MakeMaster;
use App\Models\ModelMaster;
use App\Models\PartMaster;
use App\Models\ProvinceMaster;
use App\Models\RegionMaster;
use App\Models\Roles;
use App\Models\Permissions;
use App\Models\RollMaster;
use App\Models\RollRestlen;
use App\Models\RollReturn;
use App\Models\RollTransfer;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use PhpOffice\PhpSpreadsheet\IOFactory;

/*
 * 卷膜管理
 * 退货*/
class RollsReturnController extends Controller
{

    /*
     * 退货提交、保存*/
    public function store(Request $request)
    {
        $admin_id = session('admin.id');
        $user_type = session('admin.user_type');

        if ($user_type != 2) {
            return $this->store_gly($request);
        }

        $roll_number = trim($request->input('roll_number'));
        $note = trim($request->input('note'));
        if (empty($roll_number)) {
            return back()->withInput()->with('trash', ['type'=>'error', 'content' => '卷号不能为空！']);
        }
        if (empty($note)) {
            return back()->withInput()->with('trash', ['type'=>'error', 'content' => '请补充退货说明！']);
        }
        
        $roll = RollMaster::where('roll_number', $roll_number)
            ->firstOrFail();
        if (empty($roll) || empty($roll->length)) {
            return back()->withInput()->with('trash', ['type'=>'error', 'content' => '膜卷号有误！']);
        }
        $restlen = RollRestlen::where('user_id', $admin_id)
            ->where('roll_number', $roll_number)
            ->first();
        if (empty($restlen) || empty($restlen->restlen) || $restlen->restlen != $roll->length) {
            return back()->withInput()->with('trash', ['type'=>'error', 'content' => '仅允许整卷退货！']);
        }
        DB::beginTransaction();
        try {
            $model = new RollReturn();
            $model->roll_id = $roll->id;
            $model->user_id = $admin_id;
            $model->length = $roll->length;
            $model->note = $note;
            $model->status = 0;
            $model->created_by = $admin_id;
            $model->save();
            if (empty($model)) {
                DB::rollback();//数据库回滚
                return redirect()->back()->withInput()->with('trash', ['content'=>'服务器错误，请稍后重试！', 'type'=>'error']);
            }
            $e = minusRestlen($admin_id, $roll->roll_number, $roll->length);
            if (empty($e)) {
                DB::rollback();//数据库回滚
                return redirect()->back()->withInput()->with('trash', ['content'=>'服务器错误，请稍后重试！', 'type'=>'error']);
            }
            DB::commit();
        } catch (\Exception $exception){
            DB::rollback();//数据库回滚
            return redirect()->back()->withInput()->with('trash', ['content'=>'服务器错误，请稍后重试！', 'type'=>'error']);
        }
        alogs('膜', '退货', $model->id, $model, null);
        return redirect('/admin/rolls-return')->with('trash', '已发起退货！');
    }
    public function store_gly(Request $request)
    {
        $admin_id = session('admin.id');

        $roll_number = trim($request->input('roll_number'));
        $note = trim($request->input('note'));
        $user_id = (int) trim($request->input('user_id'));

        if (empty($roll_number)) {
            return back()->withInput()->with('trash', ['type'=>'error', 'content' => '卷号不能为空！']);
        }
        if (empty($note)) {
            return back()->withInput()->with('trash', ['type'=>'error', 'content' => '请补充退货说明！']);
        }
        if (empty($user_id)) {
            return back()->withInput()->with('trash', ['type'=>'error', 'content' => '退货用户不能为空！']);
        }
        DB::beginTransaction();
        try {
            $roll = RollMaster::where('roll_number', $roll_number)
                ->first();
            if (empty($roll->id)) {
                DB::rollback();
                return back()->withInput()->with('trash', ['type' => 'error', 'content' => '卷号不存在！']);
            }
            $rs = RollRestlen::where([
                'user_id' => $user_id,
                'roll_number' => $roll_number,
            ])->first();
            if (empty($rs->restlen) || $rs->restlen != $roll->length) {
                DB::rollback();
                return back()->withInput()->with('trash', ['type' => 'error', 'content' => '用户剩余长度不符合要求！']);
            }
            $roll->status = 0;
            $roll->save();
            $model = new RollReturn();
            $model->roll_id = $roll->id;
            $model->user_id = $user_id;
            $model->length = $roll->length;
            $model->note = $note;
            $model->status = 1;
            $model->created_by = $admin_id;
            $model->updated_by = $admin_id;
            $model->save();

            $e = minusRestlen($user_id, $roll->roll_number, $roll->length);
            if (empty($e)) {
                DB::rollback();//数据库回滚
                return redirect()->back()->withInput()->with('trash', ['content'=>'服务器错误，请稍后重试！', 'type'=>'error']);
            }
            $e = plusRestlen(1, $roll_number, $roll->length);
            if (empty($e)) {
                DB::rollback();//数据库回滚
                return redirect()->back()->withInput()->with('trash', ['content'=>'服务器错误，请稍后重试！', 'type'=>'error']);
            }

            $e = addRollSales($roll->id, $model->user_id, 1, $model->length, 3);
            if (empty($e)) {
                DB::rollback();//数据库回滚
                return redirect()->back()->withInput()->with('trash', ['content'=>'服务器错误，请稍后重试！', 'type'=>'error']);
            }
            DB::commit();
        } catch (\Exception $exception){
            DB::rollback();//数据库回滚
            return redirect()->back()->withInput()->with('trash', ['content'=>'服务器错误，请稍后重试！', 'type'=>'error']);
        }

        alogs('膜', '管理员退货', $roll->id, $roll, null);
        return redirect('/admin/verify/rolls_return')->with('trash', '已发起退货！');
    }

}
